package com.khalil.kkadmin.cms.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.khalil.kkadmin.cms.domain.CmsContent;
import com.khalil.kkadmin.cms.repository.CmsContentRepository;
import com.khalil.kkadmin.cms.service.CmsContentService;
import com.khalil.kkadmin.common.util.JsonUtil;
import com.khalil.kkadmin.infrastructure.BaseService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CmsContentServiceImpl extends BaseService<CmsContentRepository, CmsContent, Long> implements CmsContentService {
    public CmsContentServiceImpl(CmsContentRepository repository) {
        super(repository);
    }

    @Override
    public List<CmsContent> list(String params) {
        JSONObject jsonObject = JSONObject.parseObject(params);
        List<Long> columnIdList = jsonObject.getJSONArray("columnIdList").toJavaList(Long.class);
        if (!columnIdList.isEmpty()) {
            return repository.findAllByColumnIdInOrderByTopFlagDescCreatedAtDesc(columnIdList);
        }
        return List.of();
    }

    @Override
    protected CmsContent str2CreateEntity(String json) {
        CmsContent content = JsonUtil.parseJson2Object(json, CmsContent.class);
        content.setId(null);
        return content;
    }

    @Override
    protected CmsContent str2UpdateEntity(String json) {
        CmsContent content = JsonUtil.parseJson2Object(json, CmsContent.class);
        return content;
    }
}
